package Utils
{
	import com.adobe.air.notification.AbstractNotification;
	
	import flash.net.SharedObject;
	
	import me2day.api.data.Me2dayPostIcon;
	import me2day.api.data.Me2dayUser;
	
	public class CommonInfoManager
	{
		[Bindable]
		public static var version:String;
		
		public static var userid:String;
		
		public static var appkey:String;
		
		public static var isUserInfoSave:Boolean;
		
		public static var isAutoLogin:Boolean;
		
		public static var isAutoLoginOld:Boolean;
		
		public static var user:Me2dayUser;
		
		public static var backgroundAlpha:Number = 0;
		
		public static var fullAuthToken:String;
		
		// 친구들은 친구목록 유형 close:친한친구, all:모든친구
		public static var friendType:String;
		
		// 친구들은 글 목록 수
		public static var friendPostCount:int;
		
		// 자동 업데이트 주기
		public static var autoUpdateDuration:int;
		
		// 자동 업데이트 댓글 갯수
		public static var autoUpdateCommentsNum:int;
		
		// 댓글 알리미 팝업 위치
		public static var notifyWindowPos:String;
		
		public static function SaveSettings():void
		{
			sharedObject.data.backgroundAlpha = backgroundAlpha;
			sharedObject.data.friendType = friendType;
			sharedObject.data.friendPostCount = friendPostCount;
			sharedObject.data.autoUpdateDuration = autoUpdateDuration;
			sharedObject.data.notifyWindowPos = notifyWindowPos;	
			sharedObject.data.autoUpdateCommentsNum = autoUpdateCommentsNum;		
		}
		
		private static var sharedObject:SharedObject = SharedObject.getLocal("me2dayAIR");
		public static function SaveUserInfo(isSave:Boolean):void
		{
			if ( isSave )
			{
				sharedObject.data.userid = userid;
				sharedObject.data.key = appkey;			
				sharedObject.data.isAutoLogin = isAutoLogin;	
				sharedObject.data.isAutoLoginOld = isAutoLoginOld;	
			}
			else
			{
				sharedObject.data.userid = "";
				sharedObject.data.key = "";
				sharedObject.data.isAutoLogin = false;
				sharedObject.data.isAutoLoginOld = false;	
			}
			sharedObject.data.isSave = isSave;
			if ( fullAuthToken )
				sharedObject.data.fullAuthToken = fullAuthToken;
		}
		
		public static function SaveAutoLogin(value:Boolean):void
		{
			isAutoLogin = value;
			sharedObject.data.isAutoLogin = value;
		}
		public static function SaveAutoLoginOld(value:Boolean):void
		{
			isAutoLoginOld = value;
			sharedObject.data.isAutoLoginOld = value;
		}
		public static function ReadSavedInfo():void
		{
			if ( sharedObject.data.hasOwnProperty("fullAuthToken") )
				fullAuthToken = sharedObject.data.fullAuthToken;	
			if ( sharedObject.data.hasOwnProperty("userid") )
				userid = sharedObject.data.userid;				
			if ( sharedObject.data.hasOwnProperty("key") )
				appkey = sharedObject.data.key;
			if ( sharedObject.data.hasOwnProperty("isSave") )
				isUserInfoSave = sharedObject.data.isSave;		
			if ( sharedObject.data.hasOwnProperty("isAutoLogin") )
				isAutoLogin = sharedObject.data.isAutoLogin;
			if ( sharedObject.data.hasOwnProperty("isAutoLoginOld") )
				isAutoLoginOld = sharedObject.data.isAutoLoginOld;
			if ( sharedObject.data.hasOwnProperty("backgroundAlpha") )
				backgroundAlpha = sharedObject.data.backgroundAlpha;	
			if ( sharedObject.data.hasOwnProperty("friendType") )
				friendType = sharedObject.data.friendType;	
			else
				friendType = "all";	
			if ( sharedObject.data.hasOwnProperty("friendPostCount") )
				friendPostCount = sharedObject.data.friendPostCount;	
			else
				friendPostCount = 6;
			if ( sharedObject.data.hasOwnProperty("autoUpdateDuration") )
				autoUpdateDuration = sharedObject.data.autoUpdateDuration;	
			else
				autoUpdateDuration = 5;
			if ( sharedObject.data.hasOwnProperty("notifyWindowPos") )
				notifyWindowPos = sharedObject.data.notifyWindowPos;	
			else
				notifyWindowPos = AbstractNotification.BOTTOM_RIGHT;
			if ( sharedObject.data.hasOwnProperty("autoUpdateCommentsNum") )
				autoUpdateCommentsNum = sharedObject.data.autoUpdateCommentsNum;	
			else
				autoUpdateCommentsNum = 6;
		}
		
		public static function DeleteFullAuthToken():void
		{
			fullAuthToken = "";
			isAutoLogin = false;
			sharedObject.data.fullAuthToken = "";
			sharedObject.data.isAutoLogin = false;
		}
		
		public static function getPostIconUrl(iconIndex:int):String
		{
			if ( user && user.postIcons )
			{
				if ( iconIndex <= user.postIcons.length )
				{
					return (user.postIcons[iconIndex-1] as Me2dayPostIcon).url;
				}
			}
			
			return "";
		}
		
		private static var defaultIconUrl:String;
		public static function getDefaultIconUrl():String
		{
			if ( !defaultIconUrl )
			{				
				defaultIconUrl = "";		
				if ( user )
				{
					if ( user.defaultIcon )
					{
						defaultIconUrl = user.defaultIcon.url;
					}
					else if ( user.postIcons && user.postIcons.length > 1 )
					{
						defaultIconUrl = (user.postIcons[0] as Me2dayPostIcon).url;
					}
				}
			}
			
			return defaultIconUrl;
		}
		
		private static var defaultIconIndex:int = -1;
		public static function getDefaultIconIndex():int
		{
			if ( defaultIconIndex == -1 )
			{
				defaultIconIndex = 0;
				if ( user )
				{
					if ( user.defaultIcon )
					{
						defaultIconIndex = user.defaultIcon.iconIndex;
					}
					else if ( user.postIcons && user.postIcons.length > 1 )
					{
						defaultIconIndex = (user.postIcons[0] as Me2dayPostIcon).iconIndex;
					}
				}				
			}
			
			return defaultIconIndex;
		}
	}
}